﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Function
{
    public partial class FunctionForm : Form
    {
        Form Index;
        bool isExit = true;
        bool isRunning = false;
        int remainTime1;
        int remainTime2;
        int remainTime3;

        public FunctionForm()
        {
            InitializeComponent();
        }

        public FunctionForm(Form Index) { 
            InitializeComponent();
            this.Index = Index;
        }

        private void Shutdown_Load(object sender, EventArgs e)
        {

        }

        private void Shutdown_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isExit) Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (isRunning)
            {
                timer1.Stop();
                isRunning = false;
                button1.Text = "延时关机";
            }
            else
            {
                if (textBox1.Text == "") remainTime1 = 0;
                else remainTime1 = int.Parse(textBox1.Text);
                if (textBox2.Text == "") remainTime2 = 0;
                else remainTime2 = int.Parse(textBox2.Text);
                if (textBox3.Text == "") remainTime3 = 0;
                else remainTime3 = int.Parse(textBox3.Text);
                isRunning = true;
                timer1.Start();
                button1.Text = "取消";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Index.Show();
            Index.Location = this.Location;
            isExit = false;
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (isRunning)
            {
                textBox1.Text = remainTime1.ToString();
                textBox2.Text = remainTime2.ToString();
                textBox3.Text = remainTime3.ToString();
                if (remainTime1 == 0 && remainTime2 == 0 && remainTime3 == 0)
                {
                    System.Diagnostics.Process.Start("shutdown", "/s /t 0");
                    Thread.Sleep(1000);
                    Application.Exit();
                }
                remainTime3--;
                if (remainTime3 < 0)
                {
                    remainTime3 = 59;
                    remainTime2--;
                    if (remainTime2 < 0)
                    {
                        remainTime2 = 59;
                        remainTime1--;
                        if (remainTime1 < 0)
                        {
                            remainTime1 = 0;
                            remainTime2 = 0;
                            remainTime3 = 0;
                        }
                    }
                }
            }
        }
    }
}
